package com.gbr.client.presentation.impl;

import java.beans.PropertyChangeListener;
import java.util.Collection;

import com.gbr.application.action.ShutdownAction;
import com.gbr.application.model.Capture;
import com.gbr.application.model.Frame;
import com.gbr.application.model.RecordingArea;
import com.gbr.client.presentation.IDesktopPresentation;
import com.ng.framework.gui.presentation.AbstractPresentationModel;
import com.ng.framework.model.IModelController;
import com.ng.framework.service.log.LogLevel;
import com.ng.framework.service.preference.IPreferenceService;

public class DesktopPresentation extends AbstractPresentationModel implements IDesktopPresentation {
    public final static String         EVENT_RECORDING_AREA  = System.getProperty("event.recordingArea");

    private IPreferenceService         prefService           = null;
    private IModelController           modelController       = null;

    private RecordingArea              recordingArea         = null;
    private CaptureModelListener       captureModelListner   = null;
    private RecordingAreaModelListener recordingAreaListener = null;

    @Override
    protected void activateComponent() {
        logService.log(this, LogLevel.TRACE, "Activating");
        captureModelListner = new CaptureModelListener(this);
        modelController.addListener(Capture.class, captureModelListner);
        recordingAreaListener = new RecordingAreaModelListener(this);
        modelController.addListener(RecordingArea.class, recordingAreaListener);
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                actionService.fireAction(new ShutdownAction());
            }
        }).start();
    }

    @Override
    protected void deactivateComponent() {
        System.out.println("*** Deactivating...");
        logService.log(getClass(), LogLevel.TRACE, "Deactivating");
        modelController.removeListener(Capture.class, captureModelListner);
        modelController.removeListener(Capture.class, recordingAreaListener);
    }

    public void add(Capture capture) {
        logService.log(getClass(), LogLevel.DEBUG, "Adding capture: " + capture);
        firePropertyChange(IDesktopPresentation.ADD, capture, null);
    }

    public void update(Capture capture) {
        logService.log(getClass(), LogLevel.DEBUG, "Updating capture: " + capture);
        firePropertyChange(IDesktopPresentation.UPDATE, capture, null);
    }

    public void remove(Capture capture) {
        logService.log(getClass(), LogLevel.DEBUG, "Removing capture: " + capture);
        firePropertyChange(IDesktopPresentation.REMOVE, capture, null);
    }

    @Override
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        super.addPropertyChangeListener(listener);

    }

    @Override
    public void shutdown() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ShutdownAction action = new ShutdownAction();
                actionService.fireAction(action);
            }
        }).start();
    }

    @Override
    public void changeCaptureSelection(Capture newValue, Capture oldValue) {
        firePropertyChange(IDesktopPresentation.CAPTURE_SELECTION_CHANGED, newValue, oldValue);
    }

    @Override
    public void changeFrameSelection(Frame newValue, Frame oldValue) {
        firePropertyChange(IDesktopPresentation.FRAME_SELECTION_CHANGED, newValue, oldValue);
    }

    @Override
    public void selectRecordingArea() {
        RecordingDelegate.selectRecordingArea(actionService);
    }

    public void setRecordingArea(RecordingArea model) {
        logService.log(getClass(), LogLevel.DEBUG, "Setting recording area to: " + model);
        this.recordingArea = model;
    }

    int temp = 0;

    @Override
    public void startRecording() {
        RecordingDelegate.startRecording(actionService, "temp" + temp++, recordingArea.getRect());
    }

    @Override
    public void stopRecording() {
        RecordingDelegate.stopRecording(actionService, recordingArea.getRect());
    }

    @Override
    public void export(final String file) {
        Collection<Capture> list = modelController.getModels(Capture.class);
        Capture capture = null;
        if (!list.isEmpty() && (capture = list.iterator().next()) != null) {
            RecordingDelegate.export(actionService, capture, file);
        } else {
            logService.log(getClass(), LogLevel.WARN, "Could not find caputre in ModelController");
        }
    }

    public final void bindService(IPreferenceService service) {
        prefService = service;
    }

    public final void unbindService(IPreferenceService service) {
        prefService = null;
    }

    public final void bindService(IModelController service) {
        modelController = service;
    }

    public final void unbindService(IModelController service) {
        modelController = null;
    }
}
